package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
    import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.system.domain.business.po.PoctMaterialInventorylogs;
import com.rzdata.poct.system.domain.business.bo.PoctMaterialInventorylogsBo;
import com.rzdata.poct.system.domain.business.vo.PoctMaterialInventorylogsVo;
import com.rzdata.poct.system.mapper.PoctMaterialInventorylogsMapper;
import com.rzdata.poct.system.service.IPoctMaterialInventorylogsService;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-16
 */
@Service
public class PoctMaterialInventorylogsServiceImpl extends ServicePlusImpl<PoctMaterialInventorylogsMapper, PoctMaterialInventorylogs, PoctMaterialInventorylogsVo> implements IPoctMaterialInventorylogsService {

    @Override
    public PoctMaterialInventorylogsVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctMaterialInventorylogsVo> queryPageList(PoctMaterialInventorylogsBo bo) {
        PagePlus<PoctMaterialInventorylogs, PoctMaterialInventorylogsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctMaterialInventorylogsVo> queryList(PoctMaterialInventorylogsBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctMaterialInventorylogs> buildQueryWrapper(PoctMaterialInventorylogsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctMaterialInventorylogs> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getInventoryNo()), PoctMaterialInventorylogs::getInventoryNo, bo.getInventoryNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChecker()), PoctMaterialInventorylogs::getChecker, bo.getChecker());
        lqw.eq(bo.getResult() != null, PoctMaterialInventorylogs::getResult, bo.getResult());
        lqw.apply(bo.getCheckTime()!=null,
            "date_format(check_time,'%y%m%d') = date_format({0},'%y%m%d')",
            bo.getCheckTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctMaterialInventorylogsBo bo) {
        PoctMaterialInventorylogs add = BeanUtil.toBean(bo, PoctMaterialInventorylogs.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctMaterialInventorylogsBo bo) {
        PoctMaterialInventorylogs update = BeanUtil.toBean(bo, PoctMaterialInventorylogs.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctMaterialInventorylogs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
